#include "configdialog.h"
#include "ui_configdialog.h"

#include <QFileDialog>
#include <QSettings>
#include <QString>

ConfigDialog::ConfigDialog(QWidget *parent) : QDialog(parent), m_ui(new Ui::ConfigDialog)
{
    m_ui->setupUi(this);

    readSettings();

    connect(m_ui->m_projectDirButton, SIGNAL(clicked()), this, SLOT(chooseWorkingDirectory()));
}

ConfigDialog::~ConfigDialog()
{
    delete m_ui;
}

void ConfigDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ConfigDialog::chooseWorkingDirectory()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Project Directory"), m_ui->m_projectDirLine->text());

    if (dir != 0)
    {
        m_ui->m_projectDirLine->setText(dir);
    }

}

void ConfigDialog::writeSettings()
{
    QSettings settings;

    settings.setValue("film/title", m_ui->m_titleLine->text());
    settings.setValue("film/author", m_ui->m_authorLine->text());
    settings.setValue("film/directory", m_ui->m_projectDirLine->text());
}

void ConfigDialog::readSettings()
{
    QSettings settings;

    m_ui->m_titleLine->setText(settings.value("film/title").value<QString>());
    m_ui->m_authorLine->setText(settings.value("film/author").value<QString>());
    m_ui->m_projectDirLine->setText(settings.value("film/directory").value<QString>());
}
